package com.cirkana.viewer.route.model.gpx;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;

import com.cirkana.serial.SerializablePassData;
import com.cirkana.serial.SerializationException;

public class Track extends SerializablePassData {

	private String name;
	private String desc;
	private String cmt;
	private ArrayList<TrackPoint> trackPoints = new ArrayList<TrackPoint>();

	// TODO: Should be protected
	public Track() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getCmt() {
		return cmt;
	}

	public void setCmt(String cmt) {
		this.cmt = cmt;
	}

	public ArrayList<TrackPoint> getTrackPoints() {
		return trackPoints;
	}

	public void setTrackPoints(ArrayList<TrackPoint> trackPoints) {
		this.trackPoints = trackPoints;
	}

	@Override
	public void serialize(DataOutputStream out) throws SerializationException {
		serializeString(this.name, out);
		serializeString(this.desc, out);
		serializeString(this.cmt, out);
		serializeSerializableList(this.trackPoints, TrackPoint.class, out);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void deserialize(DataInputStream in) throws SerializationException {
		this.name = deserializeString(in);
		this.desc = deserializeString(in);
		this.cmt = deserializeString(in);
		this.trackPoints = deserializeSerializableList(TrackPoint.class, in);
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder("{");

		s.append("name=");
		if (this.name == null)
			s.append("(null)");
		else
			s.append(this.name);
		s.append(",desc=");
		if (this.desc == null)
			s.append("(null)");
		else
			s.append(this.desc);
		s.append(",cmt=");
		if (this.cmt == null)
			s.append("(null)");
		else
			s.append(this.cmt);
		s.append(",trackPoints=");
		if (this.trackPoints == null)
			s.append("(null)");
		else {
			s.append("{");
			for (TrackPoint trackPoint : this.trackPoints) {
				if (trackPoint == null)
					s.append("(null)");
				else
					s.append(trackPoint.toString());
				s.append(",");
			}
			s.delete(s.length() - 1, s.length());
			s.append("}");
		}

		s.append("}");
		return s.toString();
	}

}
